package com.hmkj.core.api.hrpay.demo;


import com.hmkj.core.api.hrpay.utils.ConfigUtils;
import com.hmkj.core.api.hrpay.utils.SSLClient;
import com.hmkj.core.api.hrpay.utils.SignUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Scanner;


/**
 * 快捷支付demo
 * 
 * @author Administrator
 *
 */
public class QuickPayConsumeTest {

	@SuppressWarnings("resource")
	public static void main(String[] args) throws Exception {
		System.out.println("请选择你要操作的功能：");
		System.out.println("支付请选择：1，签约请选择：2");
		Scanner scanner = new Scanner(System.in);
		int change = scanner.nextInt();
		String verifyCode = "";
		String spOrderId = "";
		switch (change) {
		case 1:
			System.out.println("请输入验证码：");
			verifyCode = scanner.next();
			System.out.println("请输入商户签约订单号：");
			spOrderId = scanner.next();
			quickPay(verifyCode, spOrderId);
			break;
		case 2:
			authentication();
			break;
		case 3:
			sendTest();
			break;
		default:
			System.out.println("该功能不存在。。。a32ded82b5954a8b81b6111fd383fda1");
			break;
		}
	}

	/**
	 * 鉴权
	 * 
	 * @throws Exception
	 */
	public static String authentication() throws Exception {

		DefaultHttpClient httpClient = new SSLClient();
		HttpPost postMethod = new HttpPost(ConfigUtils.getProperty("trans_url"));
		List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
		nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));
		nvps.add(new BasicNameValuePair("version", "V1.0"));
		nvps.add(new BasicNameValuePair("productId", "0301"));
		nvps.add(new BasicNameValuePair("transId", "13"));
		nvps.add(new BasicNameValuePair("merNo", ConfigUtils.getProperty("merchant_no")));
		nvps.add(new BasicNameValuePair("orderDate", new SimpleDateFormat("yyyyMMdd").format(new Date())));
		String orderNo = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
		nvps.add(new BasicNameValuePair("orderNo", orderNo));
		nvps.add(new BasicNameValuePair("transAmt", "1"));
		nvps.add(new BasicNameValuePair("notifyUrl", "http"));
		nvps.add(new BasicNameValuePair("cardName", "11"));
		nvps.add(new BasicNameValuePair("phoneNo", "11"));
		nvps.add(new BasicNameValuePair("cardIdcardType", "01"));
		nvps.add(new BasicNameValuePair("cardIdcardNo", "11"));
		nvps.add(new BasicNameValuePair("cardType", "01"));
		nvps.add(new BasicNameValuePair("cardNo", "11"));
		nvps.add(new BasicNameValuePair("expDate", ""));
		nvps.add(new BasicNameValuePair("cvn2", ""));
		nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));

		try {
			postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse resp = httpClient.execute(postMethod);
			String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
			int statusCode = resp.getStatusLine().getStatusCode();
			if (200 == statusCode) {
				boolean signFlag = SignUtils.verferSignData(str);
				if (!signFlag) {
					System.out.println("验签失败");
					return null;
				}
				System.out.println("验签成功");
				System.out.println("商户订单号：" + orderNo);
				return orderNo;
			}
			System.out.println("返回错误码:" + statusCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}

	public static void quickPay(String verifyCode, String spOrderId) throws Exception {
		DefaultHttpClient httpClient = new SSLClient();
		HttpPost postMethod = new HttpPost(ConfigUtils.getProperty("trans_url"));
		List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>();
		nvps.add(new BasicNameValuePair("requestNo", new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())));
		nvps.add(new BasicNameValuePair("version", "V1.0"));
		nvps.add(new BasicNameValuePair("productId", "0302"));
		nvps.add(new BasicNameValuePair("transId", "15"));
		nvps.add(new BasicNameValuePair("merNo", ConfigUtils.getProperty("merchant_no")));
		nvps.add(new BasicNameValuePair("orderDate", new SimpleDateFormat("yyyyMMdd").format(new Date())));
		nvps.add(new BasicNameValuePair("orderNo", spOrderId));
		nvps.add(new BasicNameValuePair("notifyUrl", "http"));
		nvps.add(new BasicNameValuePair("transAmt", "1"));
		nvps.add(new BasicNameValuePair("commodityName", "test"));

		/* 个人信息 */
		nvps.add(new BasicNameValuePair("cardName", "11"));
		nvps.add(new BasicNameValuePair("phoneNo", "11"));
		nvps.add(new BasicNameValuePair("cardIdcardType", "01"));
		nvps.add(new BasicNameValuePair("cardType", "01"));
		nvps.add(new BasicNameValuePair("cardIdcardNo", "11"));
		nvps.add(new BasicNameValuePair("cardNo", "11"));
		// nvps.add(new BasicNameValuePair("expDate", "2012"));
		// nvps.add(new BasicNameValuePair("cvn2", "332"));

		nvps.add(new BasicNameValuePair("veriCode", verifyCode));

		nvps.add(new BasicNameValuePair("signature", SignUtils.signData(nvps)));

		try {
			postMethod.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
			HttpResponse resp = httpClient.execute(postMethod);
			String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
			int statusCode = resp.getStatusLine().getStatusCode();
			if (200 == statusCode) {
				boolean signFlag = SignUtils.verferSignData(str);
				if (!signFlag) {
					System.out.println("验签失败");
					return;
				}
				System.out.println("验签成功");
				return;
			}
			System.out.println("返回错误码:" + statusCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void sendTest() throws Exception{
		String data="cardIdcardNo=421023198811248318&cardIdcardType=01&phoneNo=13265997782&cardName=%e8%96%9b%e5%bd%a6&cardNo=6222023602090704941&cardType=01&commodityName=%e8%ae%a2%e5%8d%95%e9%87%91%e9%a2%9d%ef%bf%a50.01&merNo=850440058121001&notifyUrl=http%3a%2f%2fweipan.wsyun.com%2fashx%2fsanwingpaycallback.ashx&orderDate=20170418&orderNo=300412A341A4941201704&productId=0302&requestNo=20170418154656815&signature=AL%2fr608bgdzzYXkhrGHfyqaJAKgaz4Iq3FHcLBupVS%2baqnr5EAr4Kj%2btX1J3dBPx2KeQVA9bcoju6bLV1blzDBcbsUHSwExmnOnOmy5kvrHFKfaw0SKXDZk4eAxQBD0bHChvLwM9AEUw8Mu7KhfDEJB6F%2fpfLDjZvDhRfX5P9btxJdZBFoqagnGvZ2ZItrR5xz3Lmm17%2fTzBWRArlcf9g5Zwb2JJhxIjWg1CY80V1Q7ceqc%2fcvwvONyGFSYxALaSyR4FYKfYBT2PyfPLkwJhQ6x6T6QLpvEX3H62j66hxB2vIpAhk6tbYaI5slXoFCwUEq1CwhAucGUqBV5XDWXbxA%3d%3d&transAmt=1&transId=15&veriCode=281613&version=V1.0";
		DefaultHttpClient httpClient = new SSLClient();
		HttpPost postMethod = new HttpPost(ConfigUtils.getProperty("trans_url")+"?"+data);
		try {
			HttpResponse resp = httpClient.execute(postMethod);
			String str = EntityUtils.toString(resp.getEntity(), "UTF-8");
			int statusCode = resp.getStatusLine().getStatusCode();
			if (200 == statusCode) {
				boolean signFlag = SignUtils.verferSignData(str);
				if (!signFlag) {
					System.out.println("验签失败");
					return;
				}
				System.out.println("验签成功");
				return;
			}
			System.out.println("返回错误码:" + statusCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
